package com.bs.visitservice.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bs.visitservice.cache.CacheManager;
import com.bs.visitservice.cache.ICache;
import com.bs.visitservice.entity.StandardCode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 返回结果工具类
 */
public class ResultUtil {

    public static void main(String[] args){

    }
    //字典翻译
    public static void transfort(List<Map<String,Object>> datas,Map<String,String> transDict,boolean isRetainOri){
        List<Map<String,Object>> datasRes = new ArrayList<>();
        datas.forEach(map->{
            Map<String,Object> resMap = new HashMap<>();
            map.forEach((k,v)->{
                //获取字典类型名
                String s = transDict.get(k);
                if(s != null){
                    ICache standardCode = CacheManager.instance().getCacheProvider("standardCode");
                    //获取类型名字为s的标准代码集合
                    ArrayList<StandardCode> standardCodesList = (ArrayList<StandardCode>)standardCode.get(s);
                    if(standardCodesList != null){
                        standardCodesList.forEach(sc->{
                            String vStr = String.valueOf(v);
                            if(vStr.equals(sc.getName()) && sc.getIsValid() == 1){
                                //保留原值
                                if(isRetainOri){
                                    resMap.put(k+"_ori", v);
                                }
                                map.put(k,sc.getValue());
                            }
                        });
                    }
                }
            });
            map.putAll(resMap);
        });
    }

    /**
     * map转bean
     * @param m
     * @param c
     * @return
     */
    public static Object transObject(Map m,Class c){
        JSON json = (JSON) JSONObject.toJSON(m);
        Object t = JSONObject.toJavaObject(json, c);
        return t;
    }


}
